﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpCompare
{
    public partial class FormIgnore : Form
    {
        private SessionObj currentSession { get; set; }
        private List<SessionObj> Sessions { get; set; }
        public FormIgnore(SessionObj _currentSession, List<SessionObj> _sessions)
        {
            InitializeComponent();
            this.listViewLeft.View = View.Details;
            listViewLeft.Columns.Add("名称", 200, HorizontalAlignment.Left);
            currentSession = _currentSession;
            Sessions = _sessions;
        }

        private void FormIgnore_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            this.listViewLeft.Items.Clear();
            this.listViewLeft.BeginUpdate();
            List<string> ignorelists = new List<string>();
            if (currentSession.ignoreFiles != null)
                ignorelists = JsonConvert.DeserializeObject<List<string>>(currentSession.ignoreFiles);
            foreach (string file in ignorelists)
            {
                ListViewItem additem = new ListViewItem();
                additem.ImageIndex = 0;
                additem.Text = file;
                this.listViewLeft.Items.Add(additem);
            }
            this.listViewLeft.EndUpdate();
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            var selected = this.listViewLeft.SelectedItems;
            List<string> lists = new List<string>();
            if (currentSession.ignoreFiles != null)
                lists = JsonConvert.DeserializeObject<List<string>>(currentSession.ignoreFiles);
            foreach (ListViewItem item in selected)
            {
                lists.Remove(item.Text);
            }
            currentSession.ignoreFiles = JsonConvert.SerializeObject(lists);
            var sessions = Sessions.Where(o => o.name != currentSession.name).ToList();
            sessions.Add(currentSession);
            string jsons = JsonConvert.SerializeObject(sessions);
            FileHelper.TxtWrite(FormMain.cfgpath, jsons);
            FormMain.currentSession = currentSession;
            LoadData();
        }
    }
}
